//
//  CommissionViewPage.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/5.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "CommissionViewPage.h"
#import "BdTV.h"
#import "config.h"
#import "CommissionSubVc.h"
#import "TableMenu.h"
#import "PeriodicQueryVc.h"
#import "HourseQueryVc.h"
#import "SelecteStoreVc.h"
#import "BrokerQueryVc.h"
#import "BrokerCommissionDetailVc.h"
#import "NSString+category.h"



@interface CommissionViewPage ()<ViewPagerDataSource, ViewPagerDelegate,TableViewAction,DaySelectDelegate,HourseSelectedDelegate>
@property (nonatomic,strong)UILabel *lable1,*lable2,*lable3,*lable4,*lable5;
@property (strong, nonatomic) TableMenu *menu;
@property (strong, nonatomic) NSArray *menuArray;
@end

@implementation CommissionViewPage
@synthesize lable1,lable2,lable3,menu,menuArray,lable4,lable5;


- (void)viewDidLoad {
    
    [self initView];
    
    [super viewDidLoad];
}




-(void)initView{
    
    BdTV *title = [[BdTV alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];
    [title.backBtn setTag:11];
    [title.backBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [[title doneBtn] setImage:[UIImage imageNamed:@"icon27"] forState:UIControlStateNormal];
    [[title doneBtn]setTag:12];
    [[title doneBtn]addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [title setMianTitle:@"佣金统计"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:title];
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    menu = [TableMenu new];
    menu.myTableViewDelegate = self;
    
    self.delegate = self;
    self.dataSource = self;
}



-(void)btnOnClick:(UIButton*)sender{
    if (sender.tag == 11) {
        [self.navigationController popViewControllerAnimated:YES];
    }else if (sender.tag==12){//查询
        
        if (menuArray.count==0) {
            
            LaunchItem *item1 = [[LaunchItem alloc]init];
            [item1 setCategoryId:1];
            [item1 setName:@"周期查询"];
            
            LaunchItem *item2 = [[LaunchItem alloc]init];
            [item2 setCategoryId:2];
            [item2 setName:@"楼盘查询"];
            
            LaunchItem *item3 = [[LaunchItem alloc]init];
            [item3 setCategoryId:3];
            [item3 setName:@"门店查询"];
            
            LaunchItem *item4 = [[LaunchItem alloc]init];
            [item4 setCategoryId:4];
            [item4 setName:@"经纪人查询"];
            
            menuArray = [NSArray arrayWithObjects:item1,item2,item3,item4, nil];
            [menu setData:menuArray];
        }
        
        [menu showInView:[[UIApplication sharedApplication] keyWindow] under:sender fromBar:YES];
    }
}


#pragma mark - ViewPagerDataSource
- (NSUInteger)numberOfTabsForViewPager:(ViewPagerController *)viewPager {
    return 3;
}


- (UIView *)viewPager:(ViewPagerController *)viewPager viewForTabAtIndex:(NSUInteger)index {
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, PMW, 50)];
    

    if (index==0) {
        
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(view.frame), CGRectGetHeight(view.frame))];
        lable.backgroundColor = [UIColor clearColor];
        lable.font = [UIFont systemFontOfSize:15.0];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.text = @"全部";
//        [lable setAttributedText:[NSString makeLineStyle:@"全部" color:[UIColor redColor] fontSize:14.0]];
        lable1=lable;
        
        [view addSubview:lable];
        
    }else if(index==1){
        
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 3, CGRectGetWidth(view.frame), CGRectGetHeight(view.frame)/2)];
        lable.backgroundColor = [UIColor clearColor];
        lable.font = [UIFont systemFontOfSize:15.0];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.textColor = UIColorFromRGB(0x333333);
        lable.text = @"本期结算周期";
//        lable.text = @"本期结算周期";
        lable2=lable;
        [view addSubview:lable];
        
        
        {
            NSDate *now = [NSDate date];
            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
            NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
            
            NSInteger month = [dateComponent month];
            
            
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(view.frame)/2-2, CGRectGetWidth(view.frame), CGRectGetHeight(view.frame)/2)];
            lable.backgroundColor = [UIColor clearColor];
            lable.font = [UIFont systemFontOfSize:13.0];
            lable.textAlignment = NSTextAlignmentCenter;
            lable.textColor = UIColorFromRGB(0x333333);
            
            lable.text = [NSString stringWithFormat:@"%.1ld/1-%.1ld/30",(long)month,(long)month];
            lable.textColor = UIColorFromRGB(0x333333);
//            [lable setAttributedText:[NSString makeLineStyle:[NSString stringWithFormat:@"%.1ld/1-%.1ld/30",(long)month,(long)month] color:UIColorFromRGB(0x333333) fontSize:13.0]];

//            lable.text = [NSString stringWithFormat:@"%.1ld/1-%.1ld/30",(long)month,(long)month];
            lable4 = lable;
            [view addSubview:lable];
        }
        
       
        
    }else if (index==2){
        
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 3, CGRectGetWidth(view.frame), CGRectGetHeight(view.frame)/2)];
        lable.backgroundColor = [UIColor clearColor];
        lable.font = [UIFont systemFontOfSize:15.0];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.textColor = UIColorFromRGB(0x333333);
        lable.text = @"上期结算周期";
        lable3=lable;
        [view addSubview:lable];
        
        
        {
            NSDate *now = [NSDate date];
            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
            NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
            
            NSInteger month = [dateComponent month];

            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(view.frame)/2-2, CGRectGetWidth(view.frame), CGRectGetHeight(view.frame)/2)];
            lable.backgroundColor = [UIColor clearColor];
            lable.font = [UIFont systemFontOfSize:13.0];
            lable.textAlignment = NSTextAlignmentCenter;
            lable.textColor = UIColorFromRGB(0x333333);
            lable.text = [NSString stringWithFormat:@"%.1ld/1-%.1ld/30",month==1?12:month-1,month==1?12:month-1];
            lable.textColor = UIColorFromRGB(0x333333);
//             [lable setAttributedText:[NSString makeLineStyle:[NSString stringWithFormat:@"%.1ld/1-%.1ld/30",month==1?12:month-1,month==1?12:month-1] color:UIColorFromRGB(0x333333) fontSize:13.0]];
            lable5 = lable;
            [view addSubview:lable];
        }
    }
    
    return view;
}


- (UIViewController *)viewPager:(ViewPagerController *)viewPager contentViewControllerForTabAtIndex:(NSUInteger)index {
    
    CommissionSubVc *cvc = [[CommissionSubVc alloc]init];
    cvc.nvc = self.navigationController;
    
    if (index==1) {
        cvc.tableIndex = Commission_tab1;
//        cvc.countLab = lable2;
    }else if (index==2){
        cvc.tableIndex = Commission_tab2;
//        cvc.countLab = lable3;
    }else{//  0
        cvc.tableIndex = Commission_tab0;
//        cvc.countLab = lable1;
    }
    
    return cvc;
}


#pragma mark - ViewPagerDelegate
- (UIColor *)viewPager:(ViewPagerController *)viewPager colorForComponent:(ViewPagerComponent)component withDefault:(UIColor *)color {
    
    switch (component) {
        case ViewPagerIndicator:
            
            return [UIColor redColor];
            break;
            
        case ViewPagerTabsView:
            
            return [UIColor whiteColor];
            break;
            
        default:
            break;
    }
    
    return color;
}


-(CGFloat)viewPager:(ViewPagerController *)viewPager valueForOption:(ViewPagerOption)option withDefault:(CGFloat)value{
    
    if (option==ViewPagerOptionTabWidth) {
        return ([UIScreen mainScreen ].applicationFrame.size.width)/3 ;
    }else if(option==ViewPagerOptionTabHeight){
        return 50;
    }
    return value;
}


- (void)viewPager:(ViewPagerController *)viewPager didChangeTabToIndex:(NSUInteger)index{
    
    if (index==0) {
        [lable1 setTextColor:[UIColor redColor]];
        [lable2 setTextColor:UIColorFromRGB(0x333333)];
        [lable3 setTextColor:UIColorFromRGB(0x333333)];
        [lable4 setTextColor:UIColorFromRGB(0x333333)];
        [lable5 setTextColor:UIColorFromRGB(0x333333)];
    }else if (index==1){
        
        [lable1 setTextColor:UIColorFromRGB(0x333333)];
        [lable2 setTextColor:[UIColor redColor]];
        [lable3 setTextColor:UIColorFromRGB(0x333333)];
        [lable4 setTextColor:[UIColor redColor]];
        [lable5 setTextColor:UIColorFromRGB(0x333333)];
    }else{
        
        [lable1 setTextColor:UIColorFromRGB(0x333333)];
        [lable2 setTextColor:UIColorFromRGB(0x333333)];
        [lable3 setTextColor:[UIColor redColor]];
        [lable4 setTextColor:UIColorFromRGB(0x333333)];
        [lable5 setTextColor:[UIColor redColor]];
    }
}





#pragma mark menu Delegate
-(void)tableViewItemDidSelected:(LaunchItem*)item senderBtnTag:(int)tag{
    
    switch (item.categoryId) {
            
        case 1://周期查询
        {
            PeriodicQueryVc *vc = [PeriodicQueryVc new];
            vc.delegate =self;
              vc.hidesBottomBarWhenPushed = YES;;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        
        case 2://楼盘查询
            
        {
            HourseQueryVc *vc = [[HourseQueryVc alloc]init];
            vc.delegate = self;
              vc.hidesBottomBarWhenPushed = YES;;
            [self.navigationController pushViewController:vc animated:YES];
        }
            
            break;
            
        case 3://门店查询
        {
            __weak typeof(self) baseSelf = self;
            SelecteStoreVc *selecteStoreVc = [[SelecteStoreVc alloc]initWithBlock:^(NearbyCellData *data) {
    
                CommissionSubVc *vc = [[CommissionSubVc alloc]init];
                [vc queryDataByStoreCode:data.detail withStoreName:data.title];
                 vc.hidesBottomBarWhenPushed = YES;
                [baseSelf.navigationController pushViewController:vc animated:YES];
                
            }];
            selecteStoreVc.vcType = BrokerQueryVcType;
             selecteStoreVc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:selecteStoreVc animated:YES];
            [selecteStoreVc setTitleViewStr:@"门店查询"];
            
        }
            break;
            
        case 4://经纪人查询
        {
            BrokerQueryVc *vc = [BrokerQueryVc new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
           
            
            break;
            
        default:
            break;
    }
    
}


#pragma mark QueryVc delegate
-(void)timeSelectedFrom:(BirthDay *)begin to:(BirthDay *)end{
    
    CommissionSubVc *vc = [[CommissionSubVc alloc]init];
    [vc queryDataByBirthDay:begin to:end];;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark HourseSelecte delegate
-(void)hourseBeSelected:(int)code withHourseName:(NSString *)name{

     self.hidesBottomBarWhenPushed = YES;
    CommissionSubVc *vc = [[CommissionSubVc alloc]init];
    [vc queryDataByPrimissId:[NSString stringWithFormat:@"%d",code] withHourseName:name];
     vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


@end
